﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class SparePartApplyConfirmReceiveProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            SparePartApplyService service = new SparePartApplyService();
            SparePartApplyForm sparePartApplyForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
            serviceStationLabel.Text = sparePartApplyForm.ServiceStation.Name;
            productTypeLabel.Text = sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeName;
            sendDateLabel.Text = sparePartApplyForm.SendTime.HasValue ? sparePartApplyForm.SendTime.Value.ToString("yyyy-MM-dd") : "";
            remarkLabel.Text = sparePartApplyForm.Remark;
            rmaCodeLabel.Text = sparePartApplyForm.ApplicationProduct.ApplicationForm.ApplicationCode;
            statusLabel.Text = sparePartApplyForm.Status;
            serialNOLbl.Text = sparePartApplyForm.ApplicationProduct.ProductSerialNumber;
            applyEngineerLbl.Text = sparePartApplyForm.User.ContactPerson;
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                SparePartApplyService service = new SparePartApplyService();
                SparePartApplyForm form = service.GetSparePartApplyFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
                form.ReceiveTime = DateTime.Now;
                form.Status = "备件-待返还";
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                form.ApplicationProduct.Status = "备件维修完毕-待修";
                service.Update(form);

                Response.Write(string.Format("<script language='JavaScript'> alert( '记录已成功保存！');location.href= 'SparePartApplyListConfirmReceive.aspx' </script>"));
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "SparePartApplyConfirmReceiveProduct.aspx::saveLinkButton_Click");
                throw ex;
            }
        }
    }
}
